##############################################################################
##
## Invoke-DMV.ps1
##
## Return the results of a SSAS 2008 DMV
##
## ie:
##
##    ## Retrieve data (from the local SSAS server. default instance)
##    Invoke-DMV.ps1 -Sql 'SELECT * FROM $SYSTEM.Discover_Connections'
##
##    ## Retrieve data (from a remote server, default instance)
##    Invoke-DMV.ps1 -Sql 'SELECT * FROM $SYSTEM.Discover_Connections' -server 'WIN2K3R2EE'
##
##    ## Retrieve data (from a remote named instance)
##    Invoke-DMV.ps1 -Sql 'SELECT * FROM $SYSTEM.Discover_Connections' -server 'WIN2K3R2EE\SALES'
##
##############################################################################

param(
    [string] $server = "(local)",
    [string] $sqlCommand = $(throw "Please specify a query."),
    [string] $provider = "msolap"
  )

##write-Output "sqlCommand: $sqlCommand" ## debug

## Prepare the connection string based on information provided
$connectionString = "Provider=$provider; " +
                    "Data Source=$server; "

## Connect to the data source and open
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand, $connection
$connection.Open()

## Fetch the results, and close the connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()

## Return all of the rows from their query
$dataSet.Tables[0]
